﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WindowsApplication2.图形.imagelist_图片导出
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        ///
        /// </summary>
        int i = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                FileCtrl.CreateDirectory("Img\\");
                foreach (Image pic in this.imageList1.Images)
                {
                    pic.Save("Img\\" + i.ToString() + ".bmp");
                    i++;
                }
                Process.Start("Img\\");

                MessageBox.Show("success");
                i = 0;
            }
            catch (Exception ee) { }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                FileCtrl.CreateDirectory("Img\\");
                foreach (Image pic in this.imageList1.Images)
                {
                    string strPic = "Img\\" + i.ToString() + ".bmp";
                    pic.Save(strPic);

                    //*********************************************************
                    // 第一种，直接调用GetPixel/SetPixel方法。       我们都知道，图像在计算机中的存在形式是位图，也即一个矩形点阵，每一个点被称为一个像素。在这种方法中，我们通过GDI+中提供的GetPixel方法来读取像素的颜色，并加以计算，然后再使用SetPixel方法将计算后的颜色值应用到相应的像素上去，这样便可以得到灰度图像。       上边提到的“计算”便是指得到灰度图像的计算，其公式是：r = (像素点的红色分量 + 像素点的绿色分量 + 像素点的蓝色分量) / 3       最后得到的r便是需要应用到原像素点的值。具体的编程实现也非常的简单，只需要遍历位图的每一个像素点，然后使用SetPixel方法将上边计算得到的值应用回去即可。主要代码如下所示：
                    //*********************************************************
                    Color currentColor;
                    int r;
                    Bitmap currentBitmap = new Bitmap("Img\\" + i.ToString() + ".bmp");
                    Graphics g = Graphics.FromImage(currentBitmap);
                    for (int w = 0; w < currentBitmap.Width; w++)
                    {
                        for (int h = 0; h < currentBitmap.Height; h++)
                        {
                            currentColor = currentBitmap.GetPixel(w, h);
                            r = (currentColor.R + currentColor.G + currentColor.B) / 3;
                            currentBitmap.SetPixel(w, h, Color.FromArgb(r, r, r));
                        }
                    }
                    g.DrawImage(currentBitmap, 0, 0);

                    //FileCtrl.DirectoryDelete(strPic);
                    currentBitmap.Save("Img\\zz_" + i.ToString() + ".bmp");

                    g.Dispose();
                    //*********************************************************
                    //
                    //*********************************************************

                    i++;
                }
                Process.Start("Img\\");

                MessageBox.Show("success");
                i = 0;
            }
            catch (Exception ee) { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

            folderBrowserDialog1.SelectedPath = @"E:\编程\Debug\代码查询\工程\c#\代码查询\WindowsApplication1\bin\Debug\img";

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            DirectoryInfo directory = new DirectoryInfo(this.textBox1.Text);

            foreach (FileInfo fileInfo in directory.GetFiles())
            {
                try
                {
                    Bitmap b = new Bitmap(fileInfo.FullName);
                    ImgCtrl.ChangeHuidu(b);
                    b.Save("Img\\zz_" + StrCtrl.GetFileName(fileInfo.Name.ToString()) + ".bmp");
                }
                catch (Exception)
                {
                }
            }

            Process.Start("Img\\");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = openFileDialog1.FileName;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            //DirectoryInfo directory = new DirectoryInfo(this.textBox1.Text);

            if (this.textBox2.Text.Trim() == "")
            {
                MessageBox.Show("选择文件");
                return;
            }
            FileInfo fileInfo = new FileInfo(this.textBox2.Text);

            try
            {
                Bitmap b = new Bitmap(fileInfo.FullName);
                ImgCtrl.ChangeHuidu(b);
                b.Save("zz_" + StrCtrl.GetFileName(fileInfo.Name.ToString()) + ".bmp");
            }
            catch (Exception)
            {
            }

            Process.Start(fileInfo.Directory.ToString());
        }
    }
}